package phoenix.web.test_objects.data_providers;

import java.util.ArrayList;

import org.testng.TestNGException;

import phoenix.core.PhoenixManager;
import phoenix.core.base.extensions.TestBaseExt;
import phoenix.core.data_models.TestUserDataModel;
import phoenix.core.data_models.UserProfileDataModel;

/**
 * Created by AlexGolovko on 15.09.2014.
 */
public class MessengerDataProvider extends TestBaseExt
{
	public MessengerDataProvider(PhoenixManager p_phoenix)
	{
		super(p_phoenix);
	}

	public Object[][] registrationDataMale()
	{
		// Generate email
		String emailTemplate = manager.config().MAIL_USER;
		String emailUniquePrefix = cronos().getCurrentDate("HHmmssyyMMdd") + randomizer().getRandomInt(1, 100).toString();
		String emailName = string().getSubstringBefore(emailTemplate, "@");
		String emailDomain = string().getSubstringAfter(emailTemplate, "@");

		String email = emailName + "+" + emailUniquePrefix + "@" + emailDomain;
		// Gender
		String gender = "male";
		// Password
		String password = "qweqweQ";
		// Birth date
		String birthDay = "12";
		String birthMonth = "11";
		String birthYear = "1990";

		testUser().setGender(gender)
				.setEmail(email)
				.setPassword(password)
				.setBirthDate(birthYear + "-" + birthMonth + "-" + birthDay);

		String p_prefix = cronos().getCurrentDate("HHmmssyyMMdd") + randomizer().getRandomInt(1, 100).toString();
		p_prefix = p_prefix.substring(0, 5);
		String screenname = (manager.testUser().getGender().equals("male")) ? "Ken_" + p_prefix : "Barby_" + p_prefix;

		testUser().setScreenname(screenname);

		UserProfileDataModel profileModel = new UserProfileDataModel(manager);

		UserProfileDataModel.LookingFor lookingForSettings = profileModel.new LookingFor(testUser().getGender(), testUser().getPostcode());
		lookingForSettings.fillUpForFunnel(testUser().getGender(), testUser().getPostcode());

		UserProfileDataModel.AboutMe aboutMeSettings = profileModel.new AboutMe();
		aboutMeSettings.fillUpAboutMe();

		return new Object[][]
		{
				// Positive test
				{ gender, email, password, birthDay, birthMonth, birthYear, screenname, lookingForSettings, aboutMeSettings }
		};
	}

	public Object[][] fiveMessagesData()
	{
		// Generate email
		String emailTemplate = manager.config().MAIL_USER;
		String emailUniquePrefix = cronos().getCurrentDate("HHmmssyyMMdd") + randomizer().getRandomInt(1, 100).toString();
		String emailName = string().getSubstringBefore(emailTemplate, "@");
		String emailDomain = string().getSubstringAfter(emailTemplate, "@");

		String email = emailName + "+" + emailUniquePrefix + "@" + emailDomain;
		// Gender
		String gender = "male";
		// Password
		String password = "qweqweQ";
		// Birth date
		String birthDay = "12";
		String birthMonth = "11";
		String birthYear = "1990";

		testUser().setGender(gender)
				.setEmail(email)
				.setPassword(password)
				.setBirthDate(birthYear + "-" + birthMonth + "-" + birthDay);

		String p_prefix = cronos().getCurrentDate("HHmmssyyMMdd") + randomizer().getRandomInt(1, 100).toString();
		p_prefix = p_prefix.substring(0, 5);
		String screenname = (manager.testUser().getGender().equals("male")) ? "Ken_" + p_prefix : "Barby_" + p_prefix;

		testUser().setScreenname(screenname);

		UserProfileDataModel profileModel = new UserProfileDataModel(manager);

		UserProfileDataModel.LookingFor lookingForSettings = profileModel.new LookingFor(testUser().getGender(), testUser().getPostcode());
		lookingForSettings.fillUpForFunnel(testUser().getGender(), testUser().getPostcode());

		UserProfileDataModel.AboutMe aboutMeSettings = profileModel.new AboutMe();
		aboutMeSettings.fillUpAboutMe();

		// /

		String p_messageKey = "Hello, what do you: "
				+ randomizer().getRandomString(10)
				+ randomizer().getRandomInt(1, 1000);

		TestUserDataModel p_userSender = testUser();

		ArrayList<TestUserDataModel> p_testUserDataModels = new ArrayList<>();

		if (manager.isMD())
		{
			p_testUserDataModels.add(db().getUser("75e51dae7aef11e49ef5441ea14ed80c"));
			p_testUserDataModels.add(db().getUser("c65d156c7aeb11e49ef5441ea14ed80c"));
			p_testUserDataModels.add(db().getUser("fc2762447aea11e49ef5441ea14ed80c"));
			p_testUserDataModels.add(db().getUser("9f8c9a1b7ae911e49ef5441ea14ed80c"));
			p_testUserDataModels.add(db().getUser("ed8f79dc7ae211e49ef5441ea14ed80c"));
			p_testUserDataModels.add(db().getUser("2d7b65147ae211e49ef5441ea14ed80c"));
		}
		else if (manager.isNF())
		{
			p_testUserDataModels.add(db().getUser("3016c2c6844411e49ef5441ea14ed80c"));
			p_testUserDataModels.add(db().getUser("65b669e8844411e49ef5441ea14ed80c"));
			p_testUserDataModels.add(db().getUser("a7ed969f844411e49ef5441ea14ed80c"));
			p_testUserDataModels.add(db().getUser("dbd90100844411e49ef5441ea14ed80c"));
			p_testUserDataModels.add(db().getUser("47161c80844511e49ef5441ea14ed80c"));
			p_testUserDataModels.add(db().getUser("893bc065844511e49ef5441ea14ed80c"));
		}
		else
		{
			p_testUserDataModels.add(db().getUserBy(testModel().getSiteName(), manager.testUser().defineMembershipStatus("female"), "male", "1500"));
			p_testUserDataModels.add(db().getUserBy(testModel().getSiteName(), manager.testUser().defineMembershipStatus("female"), "male", "1600"));
			p_testUserDataModels.add(db().getUserBy(testModel().getSiteName(), manager.testUser().defineMembershipStatus("female"), "male", "1700"));
			p_testUserDataModels.add(db().getUserBy(testModel().getSiteName(), manager.testUser().defineMembershipStatus("female"), "male", "1800"));
			p_testUserDataModels.add(db().getUserBy(testModel().getSiteName(), manager.testUser().defineMembershipStatus("female"), "male", "1900"));
			p_testUserDataModels.add(db().getUserBy(testModel().getSiteName(), manager.testUser().defineMembershipStatus("female"), "male", "2000"));
		}

		if (p_testUserDataModels.size() != 6)
		{
			throw new TestNGException("Can't load users from db");
		}
		// Check for duplicated profiles
		for (int i = 0; i < p_testUserDataModels.size(); i++)
		{
			for (int j = 0; j < p_testUserDataModels.size(); j++)
			{
				if (i != j && p_testUserDataModels.get(i).getId().toString().equals(p_testUserDataModels.get(j).getId().toString()))
				{

					Integer p_time = 100;
					Integer p_actualTime = 350;
					Integer time_value = p_time + p_actualTime;

					TestUserDataModel p = db()
							.getUserBy(testModel().getSiteName(), manager.testUser().defineMembershipStatus("female"), "female", time_value.toString());

					Integer p_attempts = 0;
					while (p.getId().toString().equals(p_testUserDataModels.get(j).getId().toString()))
					{
						p_attempts++;
						p = db().getUserBy(testModel().getSiteName(), manager.testUser().defineMembershipStatus("female"), "female", time_value.toString());
						time_value = time_value + 100;

						if (p_attempts == 5)
						{
							throw new TestNGException("Unable to find unique profile after 5 attempts");
						}
					}
					p_testUserDataModels.add(p);
					p_testUserDataModels.remove(j);
				}
			}
		}

		return new Object[][]
		{
				// Positive test
				{ gender, email, password, birthDay, birthMonth, birthYear, screenname, lookingForSettings, aboutMeSettings, p_messageKey, p_userSender,
						p_testUserDataModels }
		};
	}

	public Object[][] messengerNotificationFreeData()
	{
		// Generate email
		String emailTemplate = manager.config().MAIL_USER;
		String emailUniquePrefix = cronos().getCurrentDate("HHmmssyyMMdd") + randomizer().getRandomInt(1, 100).toString();
		String emailName = string().getSubstringBefore(emailTemplate, "@");
		String emailDomain = string().getSubstringAfter(emailTemplate, "@");

		String email = emailName + "+" + emailUniquePrefix + "@" + emailDomain;
		// Gender
		String gender = "male";
		// Password
		String password = "qweqweQ";
		// Birth date
		String birthDay = "12";
		String birthMonth = "11";
		String birthYear = "1990";

		testUser().setGender(gender)
				.setEmail(email)
				.setPassword(password)
				.setBirthDate(birthYear + "-" + birthMonth + "-" + birthDay);

		String p_prefix = cronos().getCurrentDate("HHmmssyyMMdd") + randomizer().getRandomInt(1, 100).toString();
		p_prefix = p_prefix.substring(0, 5);
		String screenname = (manager.testUser().getGender().equals("male")) ? "Ken_" + p_prefix : "Barby_" + p_prefix;

		testUser().setScreenname(screenname);

		UserProfileDataModel profileModel = new UserProfileDataModel(manager);

		UserProfileDataModel.LookingFor lookingForSettings = profileModel.new LookingFor(testUser().getGender(), testUser().getPostcode());
		lookingForSettings.fillUpForFunnel(testUser().getGender(), testUser().getPostcode());

		UserProfileDataModel.AboutMe aboutMeSettings = profileModel.new AboutMe();
		aboutMeSettings.fillUpAboutMe();

		// /

		TestUserDataModel[] p_userRecipients = null;

		if (manager.isMD())
		{
			p_userRecipients = new TestUserDataModel[] {
					db().getUser("889d57ca797c11e49ef5441ea14ed80c"),
					db().getUser("d389f48d7ca811e49ef5441ea14ed80c"),
					db().getUser("92226d007ebe11e49ef5441ea14ed80c"),
			};
		}
		else if (manager.isNF())
		{
			p_userRecipients = new TestUserDataModel[] {
					db().getUser("e05f935c820b11e49ef5441ea14ed80c"),
					db().getUser("14a95a66820c11e49ef5441ea14ed80c"),
					db().getUser("4ea70bfd820c11e49ef5441ea14ed80c"),
			};
		}
		else
		{
			p_userRecipients = new TestUserDataModel[] {
					db().getUserBy(testModel().getSiteName(), manager.testUser().defineMembershipStatus("female"), "female", "1000"),
					db().getUserBy(testModel().getSiteName(), manager.testUser().defineMembershipStatus("female"), "female", "2000"),
					db().getUserBy(testModel().getSiteName(), manager.testUser().defineMembershipStatus("female"), "female", "3000"),
			};
		}

		for (int i = 0; i < p_userRecipients.length; i++)
		{
			if (p_userRecipients[i].getId() == null)
			{
				throw new TestNGException("Unable to fill users");
			}

			p_userRecipients[i].setScreenname("Barby_" + i);
			manager.backend().editPage().setUserModel(p_userRecipients[i]);
			manager.backend().searchPage().unsetUserAsScammer(p_userRecipients[i]);
			manager.backend().searchPage().unsetUserAsHot(p_userRecipients[i]);
			manager.backend().searchPage().verifyPayStatus(p_userRecipients[i]);
		}

		ArrayList<Integer> fixtureArrayPosition = new ArrayList<Integer>();
		fixtureArrayPosition.add(0, 3);
		fixtureArrayPosition.add(1, 2);
		fixtureArrayPosition.add(2, 1);

		return new Object[][]
		{
				// Positive test
				{ gender, email, password, birthDay, birthMonth, birthYear, screenname, lookingForSettings, aboutMeSettings, p_userRecipients,
						fixtureArrayPosition }
		};
	}

	public Object[][] messengerNotificationPaidData()
	{
		// Generate email
		String emailTemplate = manager.config().MAIL_USER;
		String emailUniquePrefix = cronos().getCurrentDate("HHmmssyyMMdd") + randomizer().getRandomInt(1, 100).toString();
		String emailName = string().getSubstringBefore(emailTemplate, "@");
		String emailDomain = string().getSubstringAfter(emailTemplate, "@");

		String email = emailName + "+" + emailUniquePrefix + "@" + emailDomain;
		// Gender
		String gender = "male";
		// Password
		String password = "qweqweQ";
		// Birth date
		String birthDay = "12";
		String birthMonth = "11";
		String birthYear = "1990";

		testUser().setGender(gender)
				.setEmail(email)
				.setPassword(password)
				.setBirthDate(birthYear + "-" + birthMonth + "-" + birthDay);

		String p_prefix = cronos().getCurrentDate("HHmmssyyMMdd") + randomizer().getRandomInt(1, 100).toString();
		p_prefix = p_prefix.substring(0, 5);
		String screenname = (manager.testUser().getGender().equals("male")) ? "Ken_" + p_prefix : "Barby_" + p_prefix;

		testUser().setScreenname(screenname);

		UserProfileDataModel profileModel = new UserProfileDataModel(manager);

		UserProfileDataModel.LookingFor lookingForSettings = profileModel.new LookingFor(testUser().getGender(), testUser().getPostcode());
		lookingForSettings.fillUpForFunnel(testUser().getGender(), testUser().getPostcode());

		UserProfileDataModel.AboutMe aboutMeSettings = profileModel.new AboutMe();
		aboutMeSettings.fillUpAboutMe();

		TestUserDataModel[] p_userRecipients = null;

		if (manager.isMD())
		{
			p_userRecipients = new TestUserDataModel[] {
					db().getUser("889d57ca797c11e49ef5441ea14ed80c"),
					db().getUser("d389f48d7ca811e49ef5441ea14ed80c"),
					db().getUser("92226d007ebe11e49ef5441ea14ed80c"),
			};
		}
		else if (manager.isNF())
		{
			p_userRecipients = new TestUserDataModel[] {
					db().getUser("e05f935c820b11e49ef5441ea14ed80c"),
					db().getUser("14a95a66820c11e49ef5441ea14ed80c"),
					db().getUser("4ea70bfd820c11e49ef5441ea14ed80c"),
			};
		}
		else
		{
			p_userRecipients = new TestUserDataModel[] {
					db().getUserBy(testModel().getSiteName(), manager.testUser().defineMembershipStatus("female"), "female", "1000"),
					db().getUserBy(testModel().getSiteName(), manager.testUser().defineMembershipStatus("female"), "female", "2000"),
					db().getUserBy(testModel().getSiteName(), manager.testUser().defineMembershipStatus("female"), "female", "3000"),
			};
		}

		for (int i = 0; i < p_userRecipients.length; i++)
		{
			if (p_userRecipients[i].getId() == null)
			{
				throw new TestNGException("Unable to fill users");
			}
			p_userRecipients[i].setScreenname("Barby_" + i + 1);
			manager.backend().editPage().setUserModel(p_userRecipients[i]);
			manager.backend().searchPage().unsetUserAsScammer(p_userRecipients[i]);
			manager.backend().searchPage().unsetUserAsHot(p_userRecipients[i]);
			manager.backend().searchPage().verifyPayStatus(p_userRecipients[i]);
		}

		ArrayList<Integer> fixtureArrayPosition = new ArrayList<Integer>();
		fixtureArrayPosition.add(0, 3);
		fixtureArrayPosition.add(1, 2);
		fixtureArrayPosition.add(2, 1);

		return new Object[][]
		{
				// Positive test
				{ gender, email, password, birthDay, birthMonth, birthYear, screenname, lookingForSettings, aboutMeSettings, p_userRecipients,
						fixtureArrayPosition }
		};
	}
}